package com.brspace.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.entity.HouseApproval;
import com.brspace.qualitycontrol.entity.Letter;
import com.brspace.qualitycontrol.mapper.HouseApprovalMapper;
import com.brspace.qualitycontrol.service.HouseApprovalService;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 建房审批 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-02
 */
@Service
public class HouseApprovalServiceImpl extends ServiceImpl<HouseApprovalMapper, HouseApproval> implements HouseApprovalService {
    @Resource
    private HouseApprovalMapper houseApprovalMapper;

    /**
     * 添加房屋审批信息
     *
     * @param hose
     * @return
     */
    @Override
    public boolean addHose(HouseApproval hose) {
        System.err.println(StringUtils.isNoneBlank(hose.getName()));
        if (EmptyUtil.isEmpty(hose.getName())) {
            throw new SecurityException("用户名为空");
        }
        if (EmptyUtil.isEmpty(hose.getSex())) {
            throw new SecurityException("性别为空");
        }
        if (EmptyUtil.isEmpty(hose.getCardId()) || hose.getCardId().length() < 18) {
            throw new SecurityException("身份证号小于十八位或者为空");
        }
        if (EmptyUtil.isEmpty(hose.getAddress())) {
            throw new SecurityException("家庭住址为空");
        }
        if (EmptyUtil.isEmpty(hose.getReason())) {
            throw new SecurityException("申请理由为空");
        }
        if (EmptyUtil.isEmpty(hose.getAreaing())) {
            throw new SecurityException("地基面积为空");
        }
        int i = houseApprovalMapper.insert(hose);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<HouseApproval> selectPage(Page<HouseApproval> pageParam, Object o) {
        return baseMapper.selectPage(pageParam, null);
    }

    /**
     * 获取农房申请总数
     * 同比率计算公式：rate = (本年数据 - 前一年数据) / 前一年数据
     * 环比率计算公式：rate = (本月数据 - 上个月数据) / 上个月数据
     *
     * @return
     */
    @Override
    public R houseNum() {
        Calendar cal = Calendar.getInstance();
        // 当前年
        int year = cal.get(Calendar.YEAR);
        Integer integer = baseMapper.selectCount(milkmeow(String.valueOf(year)));
        // 去年
        int year1 = cal.get(Calendar.YEAR) - 1;
        Integer integer1 = baseMapper.selectCount(milkmeow(String.valueOf(year1)));
        // 当前月
        Integer integer3 = 0;
        int month = cal.get(Calendar.MONTH) + 1;
        if (month<=9){
            integer3=baseMapper.selectCount(milkmeow(year + "-" + "0" + month));
        }else {
            integer3=baseMapper.selectCount(milkmeow(year + "-" + month));
        }
        // 上个月
        Integer integer4 =0;
        int month1 = cal.get(Calendar.MONTH);
        if (month1<=9){
            integer4 = baseMapper.selectCount(milkmeow(year + "-" + "0" + month1));
        }else {
            integer4 = baseMapper.selectCount(milkmeow(year + "-"  + month1));
        }
        // 同比
        String tong = "0.00";
        NumberFormat formatter = new DecimalFormat("0.00");
        if(integer1!=0){
            double num = integer;
            double num1 = integer1;
            double x = (num - num1) / num1 * 100;
            tong = formatter.format(x);
        }
        // 环比
        String huan = "0.00";
        if (integer4!=0){
            double num3 = integer3;
            double num4 = integer4;
            double x1 = (num3 - num4) / num4 * 100;
            huan = formatter.format(x1);
        }
        return R.ok().message("成功").data("num", baseMapper.selectCount(null)).data("tongbi", tong).data("huanbi", huan);
    }

    private Wrapper<HouseApproval> milkmeow(String year) {
        QueryWrapper<HouseApproval> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("apply_time", year);
        return queryWrapper;
    }
}
